package com.zookeeper;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
 
/**
 * Zookeeper之Watcher监听事件丢失
 *
 */
public class WatchLost {
 
	private static final String CONNECT_ADDR = "192.168.106.49:2181,192.168.106.50:2181,192.168.106.52:2181";	
    static String path = "/zk";
    static CuratorFramework client = CuratorFrameworkFactory
    		.builder()
    		.connectString(CONNECT_ADDR)
            .retryPolicy(new ExponentialBackoffRetry(1000, 3))
            .build();
 
    public static void main(String[] args) {
        try {
            client.start();
 
            final NodeCache nodeCache = new NodeCache(client, path);
            nodeCache.start();
 
            if (client.checkExists().forPath(path) == null)
                client.create().forPath(path, "0".getBytes());
 
            nodeCache.getListenable().addListener(new NodeCacheListener() {
                @Override
                public void nodeChanged() throws Exception {
                    if (nodeCache.getCurrentData() == null) {
                        System.out.println("节点被删除");
                    } else {
                        System.out.println("节点当前内容为：" + new String(nodeCache.getCurrentData().getData()));
                    }
 
                }
            });
 
            client.setData().forPath(path, "1".getBytes());
            client.setData().forPath(path, "2".getBytes());
            client.setData().forPath(path, "3".getBytes());
            client.setData().forPath(path, "4".getBytes());
            client.setData().forPath(path, "5".getBytes());
            client.setData().forPath(path, "6".getBytes());
            client.setData().forPath(path, "7".getBytes());
            client.setData().forPath(path, "8".getBytes());
            client.setData().forPath(path, "9".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
 
 
    }
}